<script setup>
import { computed } from 'vue'

// Props定义
const props = defineProps({
  // 标签内容
  label: {
    type: String,
    required: true
  },
  // 标签类型
  type: {
    type: String,
    default: 'default',
    validator: (value) => ['default', 'primary', 'success', 'warning', 'danger', 'info'].includes(value)
  },
  // 标签尺寸
  size: {
    type: String,
    default: 'medium',
    validator: (value) => ['small', 'medium', 'large'].includes(value)
  },
  // 是否可关闭
  closable: {
    type: Boolean,
    default: false
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 是否为圆角
  rounded: {
    type: Boolean,
    default: true
  },
  // 是否可点击
  clickable: {
    type: Boolean,
    default: false
  },
  // 自定义颜色
  color: {
    type: String,
    default: ''
  },
  // 自定义文字颜色
  textColor: {
    type: String,
    default: ''
  }
})

// 事件定义
const emit = defineEmits(['click', 'close'])

// 计算CSS类名
const tagClasses = computed(() => {
  return [
    'base-tag',
    `base-tag--${props.type}`,
    `base-tag--${props.size}`,
    {
      'base-tag--closable': props.closable,
      'base-tag--disabled': props.disabled,
      'base-tag--rounded': props.rounded,
      'base-tag--clickable': props.clickable && !props.disabled
    }
  ]
})

// 计算样式
const tagStyles = computed(() => {
  const styles = {}
  
  if (props.color) {
    styles.backgroundColor = props.color
  }
  
  if (props.textColor) {
    styles.color = props.textColor
  }
  
  return styles
})

// 处理点击事件
const handleClick = (event) => {
  if (props.clickable && !props.disabled) {
    emit('click', event)
  }
}

// 处理关闭事件
const handleClose = (event) => {
  event.stopPropagation()
  if (props.closable && !props.disabled) {
    emit('close')
  }
}
</script>

<template>
  <span
    :class="tagClasses"
    :style="tagStyles"
    @click="handleClick"
  >
    <span class="tag-content">{{ label }}</span>
    <span
      v-if="closable && !disabled"
      class="tag-close"
      @click="handleClose"
      title="关闭"
    >
      ×
    </span>
  </span>
</template>

<style scoped>
.base-tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 12px;
  line-height: 1.4;
  white-space: nowrap;
  transition: all 0.3s;
  border: 1px solid transparent;
}

/* 尺寸样式 */
.base-tag--small {
  padding: 1px 6px;
  font-size: 10px;
  border-radius: 6px;
}

.base-tag--medium {
  padding: 2px 10px;
  font-size: 12px;
  border-radius: 10px;
}

.base-tag--large {
  padding: 4px 12px;
  font-size: 14px;
  border-radius: 12px;
}

/* 类型样式 */
.base-tag--default {
  background-color: #f4f4f5;
  color: #606266;
}

.base-tag--primary {
  background-color: #ecf5ff;
  color: #409eff;
  border-color: #d9ecff;
}

.base-tag--success {
  background-color: #f0f9eb;
  color: #67c23a;
  border-color: #e1f3d8;
}

.base-tag--warning {
  background-color: #fdf6ec;
  color: #e6a23c;
  border-color: #faecd8;
}

.base-tag--danger {
  background-color: #fef0f0;
  color: #f56c6c;
  border-color: #fde2e2;
}

.base-tag--info {
  background-color: #edf2fc;
  color: #909399;
  border-color: #ebeef5;
}

/* 可点击样式 */
.base-tag--clickable {
  cursor: pointer;
}

.base-tag--clickable:hover {
  opacity: 0.8;
}

/* 禁用状态 */
.base-tag--disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 非圆角样式 */
.base-tag--rounded {
  border-radius: 10px;
}

.base-tag:not(.base-tag--rounded) {
  border-radius: 2px;
}

/* 标签内容 */
.tag-content {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 关闭按钮 */
.tag-close {
  margin-left: 4px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  line-height: 1;
  transition: all 0.3s;
  width: 16px;
  height: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.tag-close:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

/* 不同类型的关闭按钮悬停效果 */
.base-tag--primary .tag-close:hover {
  background-color: rgba(64, 158, 255, 0.2);
}

.base-tag--success .tag-close:hover {
  background-color: rgba(103, 194, 58, 0.2);
}

.base-tag--warning .tag-close:hover {
  background-color: rgba(230, 162, 60, 0.2);
}

.base-tag--danger .tag-close:hover {
  background-color: rgba(245, 108, 108, 0.2);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .base-tag {
    padding: 2px 8px;
    font-size: 11px;
  }
  
  .base-tag--large {
    padding: 3px 10px;
    font-size: 12px;
  }
  
  .tag-content {
    max-width: 120px;
  }
}
</style>